package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go-mall/pkg/util"
	"go-mall/service"
	"net/http"
)

func CreateAddress(c *gin.Context) {
	claims, _ := util.ParseToken(c.GetHeader("Authorization"))
	var createAddress service.AddressService
	if err := c.ShouldBind(&createAddress); err == nil {
		res := createAddress.Create(c.Request.Context(), claims.ID)
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}
func GetAddress(c *gin.Context) {
	util.ParseToken(c.GetHeader("Authorization"))
	var getAddress service.AddressService
	if err := c.ShouldBind(&getAddress); err == nil {
		res := getAddress.Show(c.Request.Context(), c.Param("id"))
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}
func ListAddress(c *gin.Context) {
	claims, _ := util.ParseToken(c.GetHeader("Authorization"))
	var listAddress service.AddressService
	if err := c.ShouldBind(&listAddress); err == nil {
		res := listAddress.List(c.Request.Context(), claims.ID)
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}

func UpdateAddress(c *gin.Context) {
	claims, _ := util.ParseToken(c.GetHeader("Authorization"))
	var updateAddress service.AddressService
	if err := c.ShouldBind(&updateAddress); err == nil {
		res := updateAddress.Update(c.Request.Context(), claims.ID, c.Param("id"))
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}

func DeleteAddress(c *gin.Context) {
	var deleteAddress service.AddressService
	if err := c.ShouldBind(&deleteAddress); err == nil {
		res := deleteAddress.Delete(c.Request.Context(), c.Param("id"))
		c.JSON(http.StatusOK, res)
	} else {
		fmt.Println("Error", err)
		c.JSON(http.StatusBadRequest, err)
	}
}
